package com.mall.system.controller;

import com.mall.common.result.Result;
import com.mall.system.entity.Menu;
import com.mall.system.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 */
@Tag(name = "菜单管理")
@Slf4j
@RestController
@RequestMapping("/system/menus")
public class MenuController {
    
    @Autowired
    private MenuService menuService;
    
    @Operation(summary = "查询菜单列表")
    @GetMapping
    public Result<List<Menu>> listMenus() {
        try {
            List<Menu> menus = menuService.list();
            return Result.success(menus);
        } catch (Exception e) {
            log.error("查询菜单列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据用户ID获取菜单")
    @GetMapping("/user/{userId}")
    public Result<List<Menu>> getMenusByUserId(@PathVariable Long userId) {
        try {
            List<Menu> menus = menuService.selectMenuByUserId(userId);
            return Result.success(menus);
        } catch (Exception e) {
            log.error("根据用户ID获取菜单异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据角色ID获取菜单")
    @GetMapping("/role/{roleId}")
    public Result<List<Menu>> getMenusByRoleId(@PathVariable Long roleId) {
        try {
            List<Menu> menus = menuService.selectMenuByRoleId(roleId);
            return Result.success(menus);
        } catch (Exception e) {
            log.error("根据角色ID获取菜单异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "创建菜单")
    @PostMapping
    public Result<Boolean> createMenu(@RequestBody Menu menu) {
        try {
            boolean result = menuService.save(menu);
            if (result) {
                return Result.success(true);
            } else {
                return Result.failed("创建菜单失败");
            }
        } catch (Exception e) {
            log.error("创建菜单异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新菜单")
    @PutMapping
    public Result<Boolean> updateMenu(@RequestBody Menu menu) {
        try {
            boolean result = menuService.updateById(menu);
            if (result) {
                return Result.success(true);
            } else {
                return Result.failed("更新菜单失败");
            }
        } catch (Exception e) {
            log.error("更新菜单异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "删除菜单")
    @DeleteMapping("/{menuId}")
    public Result<Boolean> deleteMenu(@PathVariable Long menuId) {
        try {
            boolean result = menuService.removeById(menuId);
            if (result) {
                return Result.success(true);
            } else {
                return Result.failed("删除菜单失败");
            }
        } catch (Exception e) {
            log.error("删除菜单异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}